"""异常"""
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.responses import JSONResponse

from injection_service.restful.common import Response


def on_auth_error(request: Request, exc: Exception):
    """认证异常"""
    return JSONResponse(Response(errmsg=str(exc), code=40001).serialization, status_code=401)


class SQLError(Exception):
    def __init__(self, name):
        self.name = name


def sql_error(request: Request, exc: Exception):
    """SQL异常"""
    return JSONResponse(Response(errmsg=str(exc), code=40009).serialization, status_code=409)


# 重写
async def http_exception(request, exc):
    return JSONResponse(Response(errmsg=exc.detail, code=50000).serialization, status_code=exc.status_code)

exception_handlers = {
    HTTPException: http_exception,
    SQLError: sql_error
}